package com.wwwarehouse.usercenter.fragment.businessunit;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.SlideBarView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NetUtils;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.UserFragmentPagerAdapter;
import com.wwwarehouse.usercenter.bean.BusinessInfo;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Creator by zhenwang.xiang on 2017/9/26 16:24
 * Description: 信息卡片过来的viewpager
 */

@Route(path = UserCenterConstant.PATH_BUSINESSINFO_VIEWPAGERFRAGMENT)
public class BusinessInfoViewPagerFragment extends BaseFragment {
    private CustomViewPagerInternal mCustomViewPagerInternal;
    private CirclePageIndicator mCirclePageIndicator;
    private SlideBarView mSlideBarView;
    private List<Fragment> mFragmentList;
    private StateLayout mStateLayout;
    String type = "";
    String mTitle = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_business_info_view_pager, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }


    private void initView() {
        mCustomViewPagerInternal = $(R.id.user_viewpager);
        mCirclePageIndicator = $(R.id.user_circlepage);
        mSlideBarView = $(R.id.user_slidebar);
        mStateLayout = $(R.id.user_state_layout);
        mStateLayout.showProgressView(true);
        mFragmentList = new ArrayList<>();

        if (getArguments() != null) {
            type = getArguments().getString("type");
            mTitle = getArguments().getString("cardname");
        }

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BusinessInfoViewPagerFragment) {
            mActivity.setTitle(mTitle);
        }
    }

    @Override
    public void requestDatas() {
        if (NetUtils.isHttpConnected(getActivity())) {
            requestData(type);
        } else {
            mStateLayout.showNetworkView(true);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(true);
                    requestData(type);
                }
            });
        }
    }

    private void requestData(String type) {
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        NoHttpUtils.httpPost(UserCenterConstant.GET_BUSINESSUNIT_INFO_BYTYPE, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        List<BusinessInfo> mInfoList = JSON.parseArray(commonClass.getData().toString(), BusinessInfo.class);
                        if (mInfoList != null && mInfoList.size() > 0) {
                            mStateLayout.showContentView();
                            int total = mInfoList.size();
                            int page;
                            if (total % 9 == 0) {
                                page = total / 9;
                            } else {
                                page = (int) (total / 9) + 1;
                            }
                            for (int i = 0; i < page; i++) {
                                BusunessInfoFragment busunessInfoFragment = new BusunessInfoFragment();
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("infolist", (Serializable) mInfoList);
                                bundle.putInt("page", i);
                                bundle.putString("title", mTitle);
                                busunessInfoFragment.setArguments(bundle);
                                mFragmentList.add(busunessInfoFragment);
                            }
                            mCustomViewPagerInternal.setAdapter(new UserFragmentPagerAdapter(getFragmentManager(), mFragmentList));
                            if (page <= 6) {
                                if (page > 1) {
                                    mCirclePageIndicator.setVisibility(View.VISIBLE);
                                    mSlideBarView.setVisibility(View.GONE);
                                    mCirclePageIndicator.setViewPager(mCustomViewPagerInternal, 0);
                                } else {
                                    mCirclePageIndicator.setVisibility(View.GONE);
                                    mSlideBarView.setVisibility(View.GONE);
                                }
                            } else {
                                mCirclePageIndicator.setVisibility(View.GONE);
                                mSlideBarView.setVisibility(View.VISIBLE);
                                mSlideBarView.setTotalPage(page);
                                mSlideBarView.setOnSlideChange(new SlideBarView.OnSlideChange() {
                                    @Override
                                    public void onSlideChange(int page) {
                                        mCustomViewPagerInternal.setCurrentItem(page, false);
                                    }
                                });
                                mSlideBarView.setViewPagerInternal(mCustomViewPagerInternal);
                            }
                        } else {
                            mStateLayout.showEmptyView(true);
                            mStateLayout.setEmptyListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mStateLayout.showProgressView(true);
                                    requestData(type);
                                }
                            });
                        }
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), true);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(true);
                                requestData(type);
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showSystemView(result, true);
            mStateLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(true);
                    requestData(type);
                }
            });
        }

        @Override
        public void onFinish(int what) {
        }
    };
}
